//Kruskal算法与边有关，适合于稀疏图O(eloge)
const int N = 100010,M = 200020;
int n,m;
int p[N];
struct Edge
{
    int a,b,w;
    bool operator < (const Edge&W) const
    {
        return w<W.w;
    }
}edge[M];
int find(int x)
{
    if(x!=p[x]) p[x] = find(p[x]);
    return p[x];
}
int kruskal()
{
    sort(edge,edge+m);
    for(int i = 1;i<=n;i++)
    {
        p[i] = i;
    }
    int res = 0;
    int cnt = 0;
    for(int i = 0;i<m;i++)
    {
        int a = edge[i].a;
        int b = edge[i].b;
        int w = edge[i].w;
        if(find(a)!=find(b))
        {
            p[find(a)] = find(b);
            res+=w;
            cnt++;
        }
    }
    if(cnt<n-1)
    {
        return -1;
    }
    else return res;
}

int main()
{
    cin >> n >> m;
    for(int i = 0;i<m;i++)
    {
        int a,b,w;
        cin >> edge[i].a >> edge[i].b >> edge[i].w;
    }
    int t = kruskal();
    if(t==-1)
    {
        cout << "impossible" << endl;
    }
    else
    {
        cout<<t<<endl;
    }
    return 0;
}       